﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PurchaseOrderUpdate.cs
    // 文件功能描述：编辑采购单明细信息
    // 创建标识：吕海乐2009-04-29
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.ProcureManage
{
    public partial class PurchaseOrderUpdate : BasePage
    {
        public PurchaseOrderUpdate()
        {
            this.ModuleNo = Module.单据管理.ToString();
            this.PageOperateType = OperateType.采购单管理.ToString();
        }

        IBLL_Purchase ip = Factory.BLL_Purchase();
        IBLL_Distribution id = Factory.BLL_Distribution();

        protected void Page_Load(object sender, EventArgs e)
        {
            //绑定采购单基本信息
            if (!IsPostBack)
            {
                SetBind();
                SetAllBind();
            }
        }

        /// <summary>
        /// 绑定采购单信息
        /// </summary>
        private void SetBind()
        {
            string orderId = Request.QueryString["OrderID"];
            int totalCount = 0;
            Model.View.PurchaseOrder purchaseOrder = ip.GetPurchaseOrderByWhere(new HZ.DrugTrade.Model.Query.PurchaseOrder() { OrderID = orderId, SortFields = "PlanTime DESC" }, 1, 0, out totalCount)[0];
            txtAmount.Text = purchaseOrder.TotalAmount.ToString();
            txtHospital.Text = purchaseOrder.HospitalName;
            txtPerson.Text = purchaseOrder.PlanPerson;
            lblPurseOrder.Text = purchaseOrder.OrderName;
            ViewState["orderID"] = orderId;
            ViewState["hID"] = purchaseOrder.HospitalID;

            //判断采购单的状态，若为“已提交”则屏蔽所有操作
            string status = purchaseOrder.OrderStatus.ToString();
            if (status == "0")
            {
                //txtStatus.Text = "未提交";
                ViewState["status"] = "0";
            }
            else if (status == "1")
            {
                //txtStatus.Text = "已提交";
                btnAdd.Visible = false;
                btnDel.Visible = false;
                btnModCnt.Visible = false;
                btnSubmit.Visible = false;
                //gvwPurchaseOrderDetail.Columns[12].Visible = false;
                btnDelOrder.Visible = false;
                ViewState["status"] = "1";
            }
        }

        /// <summary>
        /// 绑定所有符合条件的采购单明细
        /// </summary>
        private void SetAllBind()
        {
            string orderId = Request.QueryString["OrderID"];
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            IList<Model.View.PurchaseOrderDetail> purchaseOrderDetailList = ip.GetPurchaseOrderDetailByWhere(new HZ.DrugTrade.Model.Query.PurchaseOrderDetail() { OrderID = orderId, SortFields = " executetime asc" }, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwPurchaseOrderDetail.PageSize = AspNetPager1.PageSize;
            gvwPurchaseOrderDetail.DataSource = purchaseOrderDetailList;
            gvwPurchaseOrderDetail.DataBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetAllBind();
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnModCnt_Click(object sender, EventArgs e)
        {
            List<PurchaseOrderDetail> myList = new List<PurchaseOrderDetail>();
            for (int i = 0; i < gvwPurchaseOrderDetail.Rows.Count; i++)
            {
                System.Web.UI.HtmlControls.HtmlInputCheckBox chk = (System.Web.UI.HtmlControls.HtmlInputCheckBox)gvwPurchaseOrderDetail.Rows[i].FindControl("checkbox1");
                TextBox txtPurchaseCount = (TextBox)gvwPurchaseOrderDetail.Rows[i].FindControl("txtCount");
                Label lblPrcie = (Label)gvwPurchaseOrderDetail.Rows[i].FindControl("lblPrice");
                if (chk.Checked == true)
                {
                    //采购数量不能为“0”
                    if (txtPurchaseCount.Text.Trim() == "0")
                    {
                        HZ.Web.WebUtil.Alert("采购数量不能为0！", this);
                        txtPurchaseCount.Focus();
                        return;
                    }

                    if (string.IsNullOrEmpty(txtPurchaseCount.Text.Trim()))
                    {
                        HZ.Web.WebUtil.Alert("采购数量不能为空！", this);
                        txtPurchaseCount.Focus();
                        return;
                    }


                    PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
                    purchaseOrderDetail.OrderID = Request.QueryString["orderID"];
                    purchaseOrderDetail.Price = new HZ.Common.ConvertHelper(lblPrcie.Text).ToDecimal;
                    purchaseOrderDetail.ProcureCatalogID = gvwPurchaseOrderDetail.DataKeys[i].Values[0].ToString();
                    purchaseOrderDetail.PurchaseCount = new HZ.Common.ConvertHelper(txtPurchaseCount.Text.Trim()).ToInt32;
                    purchaseOrderDetail.Amount = purchaseOrderDetail.Price * purchaseOrderDetail.PurchaseCount;
                    purchaseOrderDetail.CompanyID_PS = gvwPurchaseOrderDetail.DataKeys[i].Values[1].ToString();
                    purchaseOrderDetail.OrderDetailID = gvwPurchaseOrderDetail.DataKeys[i].Values[2].ToString();
                    myList.Add(purchaseOrderDetail);

                }
            }

            int flag = ip.UpdatePurchaseOrderDetail_PurchaseCount_Batch(myList);
            if (flag == 1)
            {
                SetBind();
                SetAllBind();
            }
            else
            {
                PageError("更新失败！", "");
            }
        }

        /// <summary>
        /// 批量删除采购单明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {
            List<PurchaseOrderDetail> myList = new List<PurchaseOrderDetail>();
            for (int i = 0; i < gvwPurchaseOrderDetail.Rows.Count; i++)
            {
                System.Web.UI.HtmlControls.HtmlInputCheckBox chk = (System.Web.UI.HtmlControls.HtmlInputCheckBox)gvwPurchaseOrderDetail.Rows[i].FindControl("checkbox1");
                if (chk.Checked == true)
                {
                    PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
                    purchaseOrderDetail.OrderDetailID = gvwPurchaseOrderDetail.DataKeys[i].Values[2].ToString();
                    myList.Add(purchaseOrderDetail);
                }
            }

            int flag = ip.DeletePurchaseOrderDetail_Batch(myList);
            if (flag == 1)
            {
                SetBind();
                SetAllBind();
            }
            else
            {
                PageError("删除失败！", "");
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("PurchaseOrderUpdateAdd.aspx?orderID=" + ViewState["orderID"] + "&hID=" + ViewState["hID"] + "");
        }

        /// <summary>
        /// 绑定审核状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwPurchaseOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Literal ltlOrderDetailState = e.Row.FindControl("ltlOrderDetailState") as Literal;
                TextBox txtCount = e.Row.FindControl("txtCount") as TextBox;
                Label lblCount = e.Row.FindControl("lblCount") as Label;
                HtmlInputCheckBox chk = (HtmlInputCheckBox)e.Row.FindControl("checkbox1");
                if (ltlOrderDetailState.Text == "0")
                {
                    ltlOrderDetailState.Text = "未提交";
                }
                else if (ltlOrderDetailState.Text == "1")
                {
                    ltlOrderDetailState.Text = "已提交";
                }
                else if (ltlOrderDetailState.Text == "2")
                {
                    ltlOrderDetailState.Text = "已确认";
                    chk.Disabled = true;
                }
                else if (ltlOrderDetailState.Text == "3")
                {
                    ltlOrderDetailState.Text = "拒绝配送";
                    chk.Disabled = true;
                }

                if (ViewState["status"].ToString() == "1")
                {
                    txtCount.Visible = false;
                    lblCount.Visible = true;
                }
                else
                {
                    txtCount.Visible = true;
                    lblCount.Visible = false;
                }
            }
        }

        /// <summary>
        /// 批量提交采购单明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            List<PurchaseOrderDetail> detailList = new List<PurchaseOrderDetail>();
            for (int i = 0; i < gvwPurchaseOrderDetail.Rows.Count; i++)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)gvwPurchaseOrderDetail.Rows[i].FindControl("checkbox1");
                if (chk.Checked == true)
                {
                    //获取要审核的采购单明细
                    PurchaseOrderDetail detail = new PurchaseOrderDetail();
                    detail.OrderDetailID = chk.Value;
                    detail.OrderID = Request.QueryString["OrderID"];
                    detailList.Add(detail);
                }
            }
            //审核所选采购单
            if (ip.ConfirmPurchaseOrderDetail_Batch(detailList) == 1)
            {
                PageInfo("所选采购单已全部提交！", "");
            }
            else
            {
                PageError("操作失败！", "");
            }
        }

        /// <summary>
        /// 删除采购单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelOrder_Click(object sender, EventArgs e)
        {
            PurchaseOrder dPurchaseOrder = new PurchaseOrder();
            dPurchaseOrder.OrderID = Request.QueryString["OrderID"];
            if (ip.DeletePurchaseOrder(dPurchaseOrder) == 1)
            {
                PageInfo("采购单删除成功！", "PurchaseOrderCheck.aspx");
            }
            else
            {
                PageError("删除失败！", this.ReturnUrl);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            string orderId = Request.QueryString["OrderID"];
            string sql = string.Format(@"SELECT Price 采购价,ProcurePrice 采购限价,Packcount 建议采购数量,GoodsName 产品名称,Outlookc 规格型号,Material 材质,
                                        Unit 最小计量单位,PackUnit 包装单位,QualityLevel 产品类别,CompanyName_SC 生产企业,CompanyName_PS 配送企业,case OrderDetailState when 0 then '未提交' when 1 then '已提交' when 2 then '已确认' when 3 then '拒绝配送' end  as  配送状态 FROM View_PurchaseOrderDetail WHERE orderId ='{0}'", orderId);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "采购明细信息.xls");
        }

    }
}
